<template>
  <div class="menu">
    <div class="menu-logo">
      <img src="~@/assets/img/logo3.png" alt="RT 后台管理系统" />
      <span v-show="!isFold">后台管理系统</span>
    </div>
    <el-menu
      class="el-menu-vertical"
      unique-opened
      active-text-color="#000"
      :collapse="isFold"
      :collapse-transition="false"
    >
      <template v-for="subMenu in menu" :key="subMenu.id">
        <el-sub-menu :index="subMenu.id + ''">
          <template #title>
            <el-icon :size="20"
              ><component :is="subMenu.icon"></component
            ></el-icon>
            <span>{{ subMenu.name }}</span>
          </template>
          <template
            v-for="subMenuItem in subMenu.children"
            :key="subMenuItem.id"
          >
            <el-menu-item
              :index="subMenuItem.id + ''"
              @click="handleItemClick(subMenuItem)"
              :class="{ 'active-color': subMenuItem.id === activeIndex }"
              >{{ subMenuItem.name }}</el-menu-item
            >
          </template>
        </el-sub-menu>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, ref } from 'vue'
import { useStore } from '@/store'
import router from '@/router'
export default defineComponent({
  name: 'Menu',
  props: {
    isFold: {
      type: Boolean,
      default: false
    }
  },
  setup () {
    const activeIndex = ref(0)
    const store = useStore()
    // 计算属性返回菜单列，但是需要处理icon
    const menu = computed(() => {
      store.state.login.userMenu.forEach((subMenu: any, i: number) => {
        subMenu.icon = subMenu.icon?.split('-')[2]
        if (i === 3) {
          subMenu.icon = 'ChatLineSquare'
        }
      })
      return store.state.login.userMenu
    })

    const handleItemClick = (item: any) => {
      activeIndex.value = item.id
      router.push(item.url)
    }
    return {
      menu,
      activeIndex,
      handleItemClick
    }
  }
})
</script>

<style scoped lang="less">
.menu {
  .menu-logo {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    img {
      width: 60px;
      height: 50px;
    }
    span {
      font-size: 30px;
      height: 40px;
      color: #2eb2fe;
    }
  }
  .el-menu {
    width: 100%;
    border-right: 1px solid #081f33;
    .el-sub-menu {
      background-color: #001427;
    }
    .el-menu-item {
      color: #ddd;
      background-color: #081f33;
    }

    .el-menu-item:hover {
      background-color: #12608d;
    }
    :deep(.el-sub-menu__title:hover) {
      background-color: #12608d;
    }
    :deep(.el-sub-menu__title) {
      font-size: 22px;
      color: #ddd;
    }
  }
}
.active-color {
  background-color: #12608d !important;
}
.menu-subMenu {
  font-size: 14px !important;
}
</style>
